package com.example.hello.service.impl;

import com.example.hello.common.PageResult;
import com.example.hello.entity.Emp;
import com.example.hello.entity.EmpExpr;
import com.example.hello.mapper.EmpExprMapper;
import com.example.hello.mapper.EmpMapper;
import com.example.hello.service.EmpService;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

import java.time.LocalDate;
import java.util.List;

@Service
public class EmpServiceImpl implements EmpService {

    @Autowired
    private EmpMapper empMapper;
    
    @Autowired
    private EmpExprMapper empExprMapper;

    @Override
    public PageResult page(String name, Integer gender, LocalDate hireDateStart, LocalDate hireDateEnd, Integer page, Integer pageSize) {
        // 设置分页参数
        PageHelper.startPage(page, pageSize);
        
        // 执行查询
        List<Emp> empList = empMapper.list(name, gender, hireDateStart, hireDateEnd);
        
        // 获取分页信息
        Page<Emp> pageInfo = (Page<Emp>) empList;
        
        return new PageResult(pageInfo.getTotal(), pageInfo.getResult());
    }

    @Override
    public Emp getById(Integer id) {
        Emp emp = empMapper.getById(id);
        if (emp != null) {
            // 查询工作经历
            List<EmpExpr> workExperiences = empExprMapper.getByEmpId(id);
            emp.setWorkExperiences(workExperiences);
        }
        return emp;
    }

    @Override
    @Transactional
    public void add(Emp emp, MultipartFile image) {
        // 设置默认密码
        if (emp.getPassword() == null || emp.getPassword().isEmpty()) {
            emp.setPassword("123456");
        }
        
        // 处理头像上传
        if (image != null && !image.isEmpty()) {
            // 这里简化处理，实际项目中需要保存文件到服务器
            emp.setImage("/uploads/" + image.getOriginalFilename());
        }
        
        // 保存员工信息
        empMapper.insert(emp);
        
        // 保存工作经历
        if (emp.getWorkExperiences() != null && !emp.getWorkExperiences().isEmpty()) {
            for (EmpExpr workExpr : emp.getWorkExperiences()) {
                workExpr.setEmpId(emp.getId());
                empExprMapper.insert(workExpr);
            }
        }
    }

    @Override
    @Transactional
    public void update(Emp emp, MultipartFile image) {
        // 处理头像上传
        if (image != null && !image.isEmpty()) {
            emp.setImage("/uploads/" + image.getOriginalFilename());
        }
        
        // 更新员工信息
        empMapper.update(emp);
        
        // 删除原有工作经历
        empExprMapper.deleteByEmpId(emp.getId());
        
        // 保存新的工作经历
        if (emp.getWorkExperiences() != null && !emp.getWorkExperiences().isEmpty()) {
            for (EmpExpr workExpr : emp.getWorkExperiences()) {
                workExpr.setEmpId(emp.getId());
                empExprMapper.insert(workExpr);
            }
        }
    }

    @Override
    @Transactional
    public void deleteById(Integer id) {
        // 删除工作经历
        empExprMapper.deleteByEmpId(id);
        
        // 删除员工
        empMapper.deleteById(id);
    }

    @Override
    public Emp login(Emp emp) {
        return empMapper.getByUsernameAndPassword(emp);
    }
} 